import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
// import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';

import 'package:provider/provider.dart';
import 'package:scrollview_observer/scrollview_observer.dart';

import '../../../../resource/language/generated/l10n.dart';
import '../../template/barrage/barrage_list_model.dart';
import 'room_msg_item.dart';

class RoomMsgList extends StatefulWidget {
  final BarrageListModel model;

  const RoomMsgList({super.key, required this.model});

  @override
  State<RoomMsgList> createState() => _RoomMsgListState();
}

class _RoomMsgListState extends State<RoomMsgList> {
  BarrageListModel get model => widget.model;

  /// 公屏开关状态，公屏关闭是显示
  Widget get status => model.enable ? const SizedBox() : const SizedBox();
  // Container(
  //     padding: const EdgeInsets.all(6),
  //     decoration: const ShapeDecoration(
  //       shape: StadiumBorder(),
  //       color: Color(0x14FFFFFF),
  //     ),
  //     child: Text(
  //       T.current.public_screen_closed,
  //       style: TextStyle(color: Colors.white, fontSize: 11),
  //     ),
  //   );

  /// 公屏list
  Widget get listWidget => ListViewObserver(
        controller: model.observerController,
        onObserve: model.onObserve,
        child: ListView.builder(
          controller: model.scrollController,
          physics: const AlwaysScrollableScrollPhysics(
              parent: BouncingScrollPhysics()),
          shrinkWrap: false,
          padding: EdgeInsets.zero,
          itemBuilder: (context, index) {
            var reversedIndex = index;
            return Padding(
              padding: EdgeInsets.only(top: index == 0 ? 10 : 0),
              child: RoomMsgItem(msg: model.msgList[reversedIndex]),
            );
          },
          itemCount: model.msgList.length,
        ),
      );

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider.value(
      value: model,
      child: Consumer<BarrageListModel>(
        builder: (_, model, child) {
          return Padding(
            padding: const EdgeInsets.only(left: 16, right: 16, top: 10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                status,
                // if (model.msgList.isEmpty)
                //   SizedBox.shrink()
                // else
                Expanded(
                  child: Align(
                    alignment: AlignmentDirectional.topStart,
                    child: ShaderMask(
                      shaderCallback: (Rect bounds) {
                        return const LinearGradient(
                          begin: Alignment.topCenter,
                          end: Alignment.bottomCenter,
                          colors: <Color>[
                            Colors.black,
                            Colors.transparent,
                            Colors.transparent
                          ],
                          stops: [0.0, 0.1, 1.0],
                        ).createShader(bounds);
                      },
                      blendMode: BlendMode.dstOut,
                      child: listWidget,
                    ),
                  ),
                ),
              ],
            ),
          );
        },
      ),
    );
  }
}
